package com.shroo8.footballbloglib.dao.footballblogread;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.stereotype.Repository;
import com.shroo8.footballbloglib.dao.constants.DataAccessConstants;
import com.shroo8.footballbloglib.dao.util.DaoUtil;

@Repository(value="getUserIdDao")
public class GetUserIdDao {
	
	@Resource(name="dataSourceFootballblogRead")
	protected DataSource dataSource;

	@Resource(name="daoUtil")
	protected DaoUtil daoUtil;

	protected final String sql = DataAccessConstants.SP_GET_USER_ID;
	
	/*
	Stored procedure parameter list:
	(
	 	in_user_type ENUM('twitter','facebook'),
	 	in_user_external_id INT,
	 	OUT out_user_id VARCHAR(36)
	)
	 */
	public String execute(String userType, String userExternalId){
		
		List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
	    Map<String, Object> inParams = new HashMap<String, Object>();

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_TYPE, Types.CHAR));
        inParams.put(DataAccessConstants.USERS_USER_TYPE, userType);

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_EXTERNAL_ID, Types.INTEGER));
        inParams.put(DataAccessConstants.USERS_USER_EXTERNAL_ID, userExternalId);

        sqlParameters.add(new SqlOutParameter(DataAccessConstants.SP_GET_USER_ID_OUT_PARAMETER, Types.VARCHAR));
	    
        Map<String, Object> out = daoUtil.spExecute(dataSource, sql,
                sqlParameters, inParams);

		return (String) out.get(DataAccessConstants.SP_GET_USER_ID_OUT_PARAMETER);
	}
}
